package com.young.mylib.utils;

import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * Created by Young on 2017/11/13.
 */

public class TimeUtils {
    private final static String TAG = "TimeUtils";
    /**
     * yyyy-MM-dd???
     */
    public static final String DEFAULT_FORMAT_DATE = "yyyy-MM-dd";

    /**
     * 20180323112211 to 2018-03-23
     *
     * @param timeStr
     * @param oldTimeFormat
     * @param newTimeFormat
     * @return
     */
    public static String converterTimeFormat(String timeStr, String oldTimeFormat, String newTimeFormat) {
        try {
            SimpleDateFormat newformatter = new SimpleDateFormat(newTimeFormat);
            Date date = stringToDate(timeStr, oldTimeFormat);
            return newformatter.format(date);
        } catch (Exception e) {
            return timeStr;
        }
    }

    public static String getTimeFormat(String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        return formatter.format(curDate);
    }

    public static long converterTime(String timeStr) {
        try {
            return stringToLong(timeStr, "yyyy年MM月dd日 HH:mm:ss");
        } catch (ParseException e) {
            return 0;
        }
    }


    public static String getCurrentTime4file() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        return formatter.format(curDate);
    }

    public static String getCurrentTimeAnd8(String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        return formatter.format(curDate);
    }

    public static String getCurrentTimeId() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        return formatter.format(curDate);
    }

    public static String getCurrentTimeAnd8() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.HOUR, 8);
        return formatter.format(calendar.getTimeInMillis());
    }

    public static String long2str(int ts) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        formatter.setTimeZone(TimeZone.getTimeZone("GMT"));

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MINUTE, 8*60+ts);
        return formatter.format(calendar.getTimeInMillis());
    }

    public static String getTimeAnd8(long ts) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(ts);
        calendar.add(Calendar.HOUR, 8);
        return formatter.format(calendar.getTimeInMillis());
    }

    public static long getCurrTs() {
        TimeZone.setDefault(TimeZone.getTimeZone("GMT")); // 设置为GMT时区
        //  return timeString2Long("2017年9月10日 09:12:12");
        return System.currentTimeMillis();
    }

    public static long getCurrTsBeforeMinute(int minute) {
        //  return timeString2Long("2017年9月10日 09:12:12");
        //  return System.currentTimeMillis();
        TimeZone.setDefault(TimeZone.getTimeZone("GMT")); // 设置为GMT时区
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MINUTE, minute);
        return calendar.getTimeInMillis();
    }

    public static long getCurrTsBeforeHour(int minute) {
        //  return timeString2Long("2017年9月10日 09:12:12");
        //  return System.currentTimeMillis();
        TimeZone.setDefault(TimeZone.getTimeZone("GMT")); // 设置为GMT时区
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.HOUR, minute);
        return calendar.getTimeInMillis();
    }

    public static String timeLong2String(long time) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        Date curDate = new Date(time);
        return formatter.format(curDate);
    }

    public static String timeLong2String(long time, String formatStr) {
        SimpleDateFormat formatter = new SimpleDateFormat(formatStr);
        Date curDate = new Date(time);
        return formatter.format(curDate);
    }

    public static long timeString2Long(String str, String formatType) {
        try {
            Date date = stringToDate(str, formatType); // String类型转成date类型
            if (date == null) {
                return getCurrTs();
            } else {
                long currentTime = dateToLong(date); // date类型转成long类型
                return currentTime;
            }
        } catch (Exception e) {
            return getCurrTs();
        }
    }

    public static long getYMDLong(int year, int month, int day) {
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        Log.i(TAG, "CUR YEAR: " + curDate.getYear());
        if (year < 2000 && year > curDate.getYear()) {
            return -1;
        }
        if (month < 1 && month > 12) {
            return -1;
        }
        if (day < 1 && day > 31) {
            return -1;
        }
        String formatType = "yyyy年MM月dd日";
        String str = year + "年" + month + "月" + day + "日";

        try {
            return stringToLong(str, formatType);
        } catch (ParseException e) {
            e.printStackTrace();
            return -1;
        }
    }

    public static long timeString2Long(String str) {
        try {
            String formatType = "yyyy年MM月dd日 HH:mm:ss";
            Date date = stringToDate(str, formatType); // String类型转成date类型
            if (date == null) {
                return getCurrTs();
            } else {
                long currentTime = dateToLong(date); // date类型转成long类型
                return currentTime;
            }
        } catch (Exception e) {
            return getCurrTs();
        }
    }

    public static String time2Long(int year, int month, int day, String format) {
        String m = String.valueOf(month);
        if (month < 10) {
            m = "0" + month;
        }
        String d = String.valueOf(day);
        if (day < 10) {
            d = "0" + day;
        }
        String formatType = "yyyy:MM:dd";
        String str = year + ":" + m + ":" + d + " ";
        Date date = stringToDate(str, formatType);
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    public static long time2Long(int year, int month, int day, int t) {
        try {
            String formatType = "yyyy:MM:dd HH:mm:ss";
            String m = String.valueOf(month);
            if (month < 10) {
                m = "0" + month;
            }
            String d = String.valueOf(day);
            if (day < 10) {
                d = "0" + day;
            }
            String str = year + ":" + m + ":" + d + " ";
            String time = "00:00:00";
            if (t != 0) {
                time = "23:59:59";
            }
            str = str + time;
            Date date = stringToDate(str, formatType); // String类型转成date类型
            if (date == null) {
                return getCurrTs();
            } else {
                long currentTime = dateToLong(date); // date类型转成long类型
                return currentTime;
            }
        } catch (Exception e) {
            return getCurrTs();
        }
    }

    // strTime要转换的string类型的时间，formatType要转换的格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日
    // HH时mm分ss秒，
    // strTime的时间格式必须要与formatType的时间格式相同
    public static Date stringToDate(String strTime, String formatType) {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat(formatType);
            Date date = null;
            date = formatter.parse(strTime);
            return date;
        } catch (Exception e) {
            Log.i(TAG, "stringToDate null");
            return null;
        }

    }

    // strTime要转换的String类型的时间
    // formatType时间格式
    // strTime的时间格式和formatType的时间格式必须相同
    public static long stringToLong(String strTime, String formatType)
            throws ParseException {
        Date date = stringToDate(strTime, formatType); // String类型转成date类型
        if (date == null) {
            return 0;
        } else {
            long currentTime = dateToLong(date); // date类型转成long类型
            return currentTime;
        }
    }

    // currentTime要转换的long类型的时间
    // formatType要转换的string类型的时间格式
    public static String longToString(long currentTime, String formatType)
            throws ParseException {
        Date date = longToDate(currentTime, formatType); // long类型转成Date类型
        String strTime = dateToString(date, formatType); // date类型转成String
        return strTime;
    }

    // currentTime要转换的long类型的时间
    // formatType要转换的时间格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
    public static Date longToDate(long currentTime, String formatType)
            throws ParseException {
        Date dateOld = new Date(currentTime); // 根据long类型的毫秒数生命一个date类型的时间
        String sDateTime = dateToString(dateOld, formatType); // 把date类型的时间转换为string
        Date date = stringToDate(sDateTime, formatType); // 把String类型转换为Date类型
        return date;
    }

    // date要转换的date类型的时间
    public static long dateToLong(Date date) {
        return date.getTime();
    }

    // formatType格式为yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
    // data Date类型的时间
    public static String dateToString(Date data, String formatType) {
        return new SimpleDateFormat(formatType).format(data);
    }


    public static String timeAddHour(String day, int hour, String formatStr) {
        SimpleDateFormat format = new SimpleDateFormat(formatStr);
        Date date = null;
        try {
            date = format.parse(day);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        if (date == null)
            return "";
        System.out.println("front:" + format.format(date)); //显示输入的日期
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR, hour);// 24小时制
        date = cal.getTime();
        System.out.println("after:" + format.format(date));  //显示更新后的日期
        cal = null;
        return format.format(date);
    }

    public static String timeAddHour(String day, int hour) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = format.parse(day);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        if (date == null)
            return "";

        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR, hour);// 24小时制
        date = cal.getTime();

        cal = null;
        return format.format(date);
    }

    public static long timeAddHour(long time, int hour) {
        Date date = new Date(time);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR, hour);// 24小时制
        date = cal.getTime();

        cal = null;
        return dateToLong(date);
    }

    public static String formatTime(String timeStr, String formatStr1, String formatStr2) {
        SimpleDateFormat format1 = new SimpleDateFormat(formatStr1);
        SimpleDateFormat format2 = new SimpleDateFormat(formatStr2);
        Date date = null;
        try {
            date = format1.parse(timeStr);
            return format2.format(date);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return timeStr;
    }

}
